<template>
	<!-- 场景联动页面 -->
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<el-row class="ml10" v-show="showSearch">
				<el-form :inline="true" :model="state.queryForm" @keyup.enter="getDataList" ref="queryRef">
					<el-form-item label="场景名称" prop="sceneName">
						<el-input placeholder="请输入场景名称" style="max-width: 180px" v-model="state.queryForm.sceneName" />
					</el-form-item>
					<el-form-item>
						<el-button @click="getDataList" icon="search" type="primary">
							{{ $t('common.queryBtn') }}
						</el-button>
						<el-button @click="resetQuery" icon="Refresh">{{ $t('common.resetBtn') }}</el-button>
					</el-form-item>
				</el-form>
			</el-row>
			<el-row>
				<div class="mb8" style="width: 100%">
					<el-button v-auth="'exsecs_sceneLinkage_add'" @click="formDialogRef.openDialog()" class="ml10" type="primary">
						{{ $t('common.addBtn') }}
					</el-button>
					<!-- 导出 -->
					<!-- @exportExcel="exportExcel" -->
					<right-toolbar
						:export="'sys_client_del'"
						@queryTable="getDataList"
						class="ml10"
						style="float: right; margin-right: 20px"
						v-model:showSearch="showSearch"
					></right-toolbar>
				</div>
			</el-row>
			<el-table
				:data="state.dataList"
				style="width: 100%"
				row-key="id"
				v-loading="state.loading"
				border
				:cell-style="tableStyle.cellStyle"
				:header-cell-style="tableStyle.headerCellStyle"
			>
				<!-- 复选框 -->
				<!-- <el-table-column align="center" type="selection" width="40" /> -->
				<el-table-column :label="t('scene.index')" type="index" width="60" />
				<el-table-column label="场景名称" prop="sceneName" show-overflow-tooltip />
				<el-table-column label="场景描述" prop="description" show-overflow-tooltip />
				<el-table-column label="触发方式" show-overflow-tooltip width="100">
					<template #default="scope">
						<div style="" v-if="scope.row.type == 0">设备</div>
						<div style="" v-if="scope.row.type == 1">定时</div>
					</template>
				</el-table-column>
				<el-table-column label="业务类型" width="120px">
					<template #default="scope">
						<div v-if="scope.row.actionType == 0">联动</div>
						<div v-if="scope.row.actionType == 1">告警</div>
					</template>
				</el-table-column>
				<el-table-column label="创建时间" prop="createTime" show-overflow-tooltip width="180" />
				<el-table-column label="运行状态" width="100px">
					<template #default="scope">
						<div v-if="scope.row.status == 1">
							<el-tag type="success">运行中</el-tag>
						</div>
						<div v-if="scope.row.status == 2"><el-tag type="danger">未启动</el-tag></div>
					</template>
				</el-table-column>
				<!-- <el-table-column :label="t('scene.runningState')" prop="runningState" show-overflow-tooltip /> -->
				<el-table-column  width="160">
					<!-- <template #default="scope">
						<el-button
							v-if="scope.row.status == 2"
							@click="formDialogRef.openDialog(scope.row.linkageId)"
							text
							type="primary"
							v-auth="'exsecs_sceneLinkage_edit'"
							>{{ $t('common.editBtn') }}
						</el-button>
						<el-button @click="handleStartUp(scope.row)" text type="primary" v-if="scope.row.status == 2">
							启动
						</el-button>
						<el-button @click="handleStartUp(scope.row)" text type="primary" v-if="scope.row.status == 1">
							停止
						</el-button>
						<el-button @click="handleTrigger(scope.row.linkageId)" text type="primary" v-if="scope.row.status == 1">
							触发
						</el-button>
						<el-button @click="formDialogDetailsRef.openDialog(scope.row.linkageId)" text type="primary">{{ $t('common.viewBtn') }} </el-button>
						<el-button @click="permessionRef.openDialog(scope.row.linkageId)" text type="primary">
							日志
						</el-button>
						<el-button icon="delete" @click="handleDelete(scope.row.linkageId)" text type="primary" v-auth="'exsecs_sceneLinkage_del'">
							删除
						</el-button>
					</template> -->
					<template #default="scope">
						<!-- 启动 -->
						<el-button icon="VideoPlay" @click="handleStartUp(scope.row)" text type="primary" v-if="scope.row.status == 2">
							启动
						</el-button>
						<!-- 停止 -->
						<el-button icon="VideoPause" @click="handleStartUp(scope.row)" text type="primary" v-if="scope.row.status == 1">
							停止
						</el-button>
						<!-- 触发 -->
						<el-button
							icon="RefreshRight"
							@click="handleTrigger(scope.row.linkageId)"
							text
							type="primary"
							:disabled="scope.row.status == 1 ? false : true"
						>
							触发
						</el-button>
					</template>
				</el-table-column>
				<el-table-column :label="$t('common.action')" width="260">
					<!-- <template #default="scope">
						<el-button
							v-if="scope.row.status == 2"
							@click="formDialogRef.openDialog(scope.row.linkageId)"
							text
							type="primary"
							v-auth="'exsecs_sceneLinkage_edit'"
							>{{ $t('common.editBtn') }}
						</el-button>
						<el-button @click="handleStartUp(scope.row)" text type="primary" v-if="scope.row.status == 2">
							启动
						</el-button>
						<el-button @click="handleStartUp(scope.row)" text type="primary" v-if="scope.row.status == 1">
							停止
						</el-button>
						<el-button @click="handleTrigger(scope.row.linkageId)" text type="primary" v-if="scope.row.status == 1">
							触发
						</el-button>
						<el-button @click="formDialogDetailsRef.openDialog(scope.row.linkageId)" text type="primary">{{ $t('common.viewBtn') }} </el-button>
						<el-button @click="permessionRef.openDialog(scope.row.linkageId)" text type="primary">
							日志
						</el-button>
						<el-button icon="delete" @click="handleDelete(scope.row.linkageId)" text type="primary" v-auth="'exsecs_sceneLinkage_del'">
							删除
						</el-button>
					</template> -->
					<template #default="scope">
						<el-button
							icon="Edit"
							:disabled="scope.row.status == 2 ? false : true"
							@click="formDialogRef.openDialog(scope.row.linkageId)"
							text
							type="primary"
							v-auth="'exsecs_sceneLinkage_edit'"
							>编辑
						</el-button>
						<!-- 查看 -->
						<el-button icon="View" @click="formDialogDetailsRef.openDialog(scope.row.linkageId)" text type="primary"
							>{{ $t('common.viewBtn') }}
						</el-button>
						<!-- 日志 -->
						<el-button icon="Document" @click="permessionRef.openDialog(scope.row.linkageId)" text type="primary"> 日志 </el-button>
						<!-- 删除 -->
						<el-button icon="delete" @click="handleDelete(scope.row.linkageId)" text type="primary" v-auth="'exsecs_sceneLinkage_del'">
							删除
						</el-button>
					</template>
				</el-table-column>
				<template #empty>
					<div class="zanwu_img">
						<img src="../../../assets/img/zanwu.png" alt="" />
						<div class="font">暂无数据</div>
					</div>
				</template>
			</el-table>
			<pagination @current-change="currentChangeHandle" @size-change="sizeChangeHandle" v-bind="state.pagination" />
		</div>
		<!-- 编辑、新增  -->
		<form-dialog @refresh="getDataList()" ref="formDialogRef" />
		<!-- 详情 -->
		<Details-page @refresh="getDataList()" ref="formDialogDetailsRef" />
		<form-dialog-log @refresh="getDataList()" ref="permessionRef" />
	</div>
</template>

<script lang="ts" name="systemSysOauthClientDetails" setup>
import { BasicTableProps, useTable } from '/@/hooks/table';
import { delRuleObj, fetchList, refreshCache, updateStatus, getTrigger } from '/@/api/scene/sceneLinkage';
import { useMessage, useMessageBox } from '/@/hooks/message';
import { useDict } from '/@/hooks/dict';
import { useI18n } from 'vue-i18n';
import { useRouter } from 'vue-router';

// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));
const DetailsPage = defineAsyncComponent(() => import('./details.vue'));
const FormDialogLog = defineAsyncComponent(() => import('./formLog.vue'));
const { t } = useI18n();
// 定义查询字典

// const { grant_types } = useDict('grant_types');
// 定义变量内容
const formDialogRef = ref();
const permessionRef = ref();
const formDialogDetailsRef = ref(); // 详情
const queryRef = ref();
// 搜索变量
const showSearch = ref(true);
// 多选变量
// const selectObjs = ref([]) as any;
// const multiple = ref(true);

const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {
		actionType: '1',
	},
	pageList: fetchList,
	// descs: ['id'],
});

const { getDataList, currentChangeHandle, sizeChangeHandle, downBlobFile, tableStyle } = useTable(state);

// // 导出excel
// const exportExcel = () => {
// 	downBlobFile('/admin/client/export', Object.assign(state.queryForm, { ids: selectObjs }), 'client.xlsx');
// };

const resetQuery = () => {
	queryRef.value.resetFields();
	// state.queryForm = {};
	// selectObjs.value = [];
	getDataList();
};

// 删除操作
const handleDelete = async (id: string) => {
	try {
		await useMessageBox().confirm(t('common.delConfirmText'));
	} catch {
		return;
	}
	try {
		await delRuleObj(id);
		getDataList();
		useMessage().success(t('common.delSuccessText'));
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};

// 启动 / 启动
const handleStartUp = async (val) => {
	try {
		await useMessageBox().confirm(t('确认要执行该操作吗？'));
	} catch {
		return;
	}
	try {
		await updateStatus({
			linkageId: val.linkageId,
			status: val.status == 2 ? '1' : '2',
		});
		getDataList();
		useMessage().success(t('操作成功'));
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};
// 触发
const handleTrigger = async (val) => {
	// try {
	// 	await getTrigger(val);
	// 	getDataList();
	// 	useMessage().success(t('触发成功'));
	// } catch (err: any) {
	// 	useMessage().error(err.msg);
	// }
	try {
		await useMessageBox().confirm(t('确认要执行该操作吗？'));
	} catch {
		return;
	}
	try {
		await getTrigger(val);
		getDataList();
		useMessage().success(t('触发成功'));
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};
const router = useRouter();

const route = useRoute();
onMounted(() => {
	// setTimeout(() => {
	// 	if (route.query.linkageId) {
	// 		// console.log('有值')
	// 		formDialogRef.value.openDialog(route.query.linkageId);
	// 	} else {
	// 		// console.log("无值")
	// 	}
	// }, 350);
});
</script>
<style lang="scss" scoped>
.zanwu_img {
	img {
		margin: auto;
	}
	.font {
		display: flex;
		justify-content: center;
		margin-top: 10px;
		color: #546c88;
	}
}
</style>
